Var olan sqlite veritabanından veri çekme
16.01.2014 - 08:10
Merhaba. Benim elimde sqlite manager ile oluşturduğum veritabanı dosyası var ve bunu projeme dahil ettim. Ben şimdi bu veritabanından veri çekip listView'de göstermek istiyorum.
[code] package com.example.databasestokdeneme; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DataBaseHelper extends SQLiteOpenHelper { private static String DB_PATH = "/data/data/com.example.databasestokdeneme/databases/"; private static String DB_NAME = "stokTest"; private SQLiteDatabase myDatabase; private final Context myContext; public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ database already exist } else { this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } } private void copyDataBase() throws IOException { // TODO Auto-generated method stub //InputStream dbInput = myContext.getAssets().open(DB_NAME); String outFile = DB_PATH + DB_NAME; OutputStream dbOutput = new FileOutputStream(outFile); byte[] buffer = new byte[1024]; int length; // while ((length = dbInput.read(buffer))>0) { // dbOutput.write(buffer,0,length); InputStream myInput = myContext.getAssets().open("mydb.sqlite"); while ((length = myInput.read(buffer)) > 0) { dbOutput.write(buffer, 0, length); } myInput.close(); dbOutput.flush(); dbOutput.close(); } private boolean checkDataBase() { SQLiteDatabase check = null; try { String dbPath = DB_PATH+DB_NAME; check = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY); } catch (Exception e) { // TODO: handle exception } if (check!=null) { check.close(); } return check != null ? true : false; } public void openDB() throws SQLException{ String dbPath = DB_PATH+DB_NAME; myDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public void onCreate(SQLiteDatabase db) { } @Override public synchronized void close() { if(myDatabase != null) myDatabase.close(); super.close(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public class Kullanicilar extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.kullanici); ListView kullanici = (ListView)findViewById(R.id.listView1); } } [/code]
146
Görüntülenme
0 Beğeni